//
//  MainViewController.m
//  iCarStyle
//
//  Created by liunian on 13-9-5.
//  Copyright (c) 2013年 liunian. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "MainViewController.h"
#import "DataEnvironment.h"
#import "WeatherView.h"
#import "DeskView.h"
#import "AppDelegate.h"
#import "NewMenuTab.h"
#import "CustomTabModel.h"
#import "PictureView.h"
#import "ArticleManager.h"
#import "ALAlertBanner.h"
#import "ImageViewController.h"

#define kDefaultAnimationIntervalSlow     1.0f
#define kDefaultAnimationIntervalMedium   0.5f
#define kDefaultAnimationIntervalFast     0.3f
#define kDefaultAnimationIntervalFaster   0.2f

@interface MainViewController ()<UIScrollViewDelegate,NewMenuTabDelegate,MKEnvObserverApplicationProtocol,PictureViewDelegate>{
    
}
@property (nonatomic, strong) NSArray *cNames;
@property (nonatomic, retain) NSMutableArray *views;
@property (nonatomic, retain) NewMenuTab *menu;
@property (nonatomic, retain) NSMutableArray *menuData;
@end

@implementation MainViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
        self.title = @"图说";
    }
    return self;
}
- (NSString *)tabTitle
{
	return self.title;
}
- (NSString *)tabImageName
{
	return @"image-9";
}
- (void)viewDidLoad
{

    [super viewDidLoad];
    MKAddApplicationObserver(self);
    [self.view setBackgroundColor:[UIColor flatWhiteColor]];
    _cNames = [NSArray arrayWithObjects:@"图解新车",@"酷车鉴赏",@"汽车生活",@"靓丽车模", nil];
    [self initPicturesControllers];
    //menu
    self.menu.menus = self.menuData;
    [self.view addSubview:self.menu];
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 70000

#else
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}
#endif
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
//    [appDelegate hideTabbar:NO];

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

#pragma mark PictureViewDelegate
- (void)toItemViewControllerWithSource:(NSArray *)source  picture:(PicModel*)picture;{
    ImageViewController *imageViewController = [[ImageViewController alloc] init];
    [imageViewController setContentWithSources:source picture:picture fromSubject:YES];
    imageViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:imageViewController animated:YES];
}
#pragma mark MKEnvObserverApplicationProtocol
- (void)mkEnvObserverApplicationWillEnterForeground:(NSNotification *)notification{
}
- (void)initPicturesControllers{
    _views = [NSMutableArray arrayWithCapacity:self.cNames.count];
    for (int i = 0 ; i < [self.cNames count]; i++) {
        PictureView *view = [[PictureView alloc] initWithFrame:self.menu.viewFrame Catalog:i+8];
        view.pDelegate = self;
        [view setBackgroundColor:[UIColor blackColor]];
        [self.views addObject:view];
    }
}
- (void)doTabItemClick:(NewMenuTab*)sender withItem:(CustomTabItem *)item
{
    BOOL isNew = NO;
    PictureView *curView        = nil;
    NSInteger nselectIndex              = sender.ncurrentSelectedIndex;
    CustomTabItem_DataItem *dataItem    = [self.menuData objectAtIndex:nselectIndex];
    curView           = [dataItem.item_data objectForKey:KEY_DATAVIEW];
    if(![self.menu isBodyContainView:curView])
        {
        isNew = YES;
        }
    if(isNew)
        {
        [self.menu addSubviewToBody:curView];
        [curView refreshData];
        [curView hideNoDataView];
        [curView layoutSubviews];
        }
    else
        {//由于nodataview是公用的，因此这里需要每次显示之前控制显示与隐藏
            [curView changeNoDataViewState];
        }
}

- (NewMenuTab*)menu
{
    if(!_menu)
        {
        CGRect menu_frame       = CGRectMake(0, _kSpaceHeight, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 49 - _kSpaceHeight);
        _menu = _ALLOC_OBJ_WITHFRAME_(NewMenuTab, menu_frame);
        _menu.menuHead.backgroundColor   = [UIColor flatBlackColor];
        _menu.isNeedDefaultSelectedEvent = YES;
        _menu.isCoverage        = YES;
        _menu.font_size         = 14;
        _menu.menuHeadHeight    = 36;
        _menu.isNeedSpanLine    = NO;
        //        _menu.imageSignName     = @"bg_menu_bottom.png";
        _menu.color_normal      = [UIColor clearColor];
        _menu.color_selected    = [UIColor clearColor];
        _menu.color_textNormal      = [UIColor flatGrayColor];
        _menu.color_textSelected    = [UIColor flatWhiteColor];
        _menu.backgroundColor   = [UIColor flatWhiteColor];
        _menu.delegate  = self;
        }
    return _menu;
}

//获取菜单数据源
- (NSMutableArray*)menuData
{

    if(!_menuData)
        {
        _menuData = _ALLOC_OBJ_(NSMutableArray);

        int count = _cNames.count;
        for(NSInteger i=0; i<count; ++i)
            {
            NSString *tmpTitle      = [self.cNames objectAtIndex:i];
            NSString *tmpView       = [self.views objectAtIndex:i];
            NSDictionary *dicItem   = [NSDictionary dictionaryWithObjectsAndKeys:tmpTitle, KEY_DATATITLE, tmpView, KEY_DATAVIEW, nil];
            CustomTabItem_DataItem *item = [[CustomTabItem_DataItem alloc] initWithData:dicItem];
            item.key_data = KEY_DATATITLE;
            [_menuData addObject:item];
            }
        }
    return _menuData;
}

@end
